package com.satan.controller;

import com.satan.dto.PageDto;
import com.satan.entity.SysRole;
import com.satan.entity.response.ResCode;
import com.satan.entity.response.ResultEntity;
import com.satan.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SysRoleController
 * @Description TODO
 * @date 2023/11/24 21:41
 * @Version 1.0
 */
@RestController
@RequestMapping("role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 获取全部角色
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:role:query')")
    public ResultEntity list(){
        List<SysRole> roleList = sysRoleService.list();
        return ResultEntity.success(roleList);
    }

    @PostMapping("/list/page")
    @PreAuthorize("hasAuthority('system:role:query')")
    public ResultEntity pageList(@RequestBody PageDto pageDto){
        Map<String, Object> resultMap = sysRoleService.getPageList(pageDto);
        return ResultEntity.success(resultMap);
    }
    /**
     * 保存或修改角色
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:role:add')" + "||" + "hasAuthority('system:role:edit')")
    public ResultEntity saveOrUpdateRole(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.saveOrUpdateRole(sysRole);
        if(b){
            return ResultEntity.success(null);
        }else {
            return ResultEntity.fail(ResCode.UPDATE_ERROR);
        }
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:role:query')")
    public ResultEntity findById(@PathVariable(value = "id")Integer id){
        SysRole sysRole = sysRoleService.getById(id);
        return ResultEntity.success(sysRole);
    }
    /**
     * 删除或批量删除角色
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('system:role:delete')")
    public ResultEntity deleteRoleByIds(@RequestBody Long[] ids){
        boolean b = sysRoleService.deleteRoleByIds(ids);
        if(b){
            return ResultEntity.success(null);
        }else {
            return ResultEntity.fail(ResCode.DELETE_ERROR);
        }
    }




}
